RM.Pages.Functions.Groups_Edit = {
    CurrentLanguage : RM.Language,
    Main: function (group) {
        RM.Pages.Groups_Edit_Panel_Units_Json.reload({
            callback : function(){
                Ext.getCmp('edit_group_main_unit_id').setValue(group.main_unit_id);
                Ext.getCmp('edit_group_main_unit_id').setRawValue(group.main_unit_name);
            }
        });

        Ext.getCmp('edit_groups_iso').setValue(this.CurrentLanguage);
        Ext.getCmp('edit_group[id]').setValue(group.id);
        Ext.getCmp('edit_group[name]').setValue(group.name);        
        Ext.getCmp('edit_group_published').setValue(group.published);
        Ext.getCmp('toolbar_panel').layout.setActiveItem('rm_pages_groups_edit_toolbar');
        Ext.getCmp('content-panel').layout.setActiveItem('rm_pages_groups_edit');
    },
    Request: function(groupID, iso){
        this.CurrentLanguage = iso;
        var myMask = new Ext.LoadMask('content-panel', {msg:RM.Translate.Common.PleaseWait});
        myMask.show();
        Ext.Ajax.request({
            url: RM.Common.AssembleURL({
                controller: 'Groups_Edit',
                action: 'editjson',
                parameters: [{
                    name: 'id',
                    value: groupID
                },{
                    name: 'iso',
                    value: iso
                }]
            }),
            method: 'POST',
            success: function(responseObject) {
                eval('RM.Pages.Functions.Groups_Edit.Main('+responseObject.responseText+');');
                myMask.hide();
            },
            failure: function() {
                Ext.Msg.alert(RM.Translate.Common.Status, RM.Translate.Common.UnableToShow);
                myMask.hide();
            }
        });
    },
    LanguageChange: function(combo, record, index){
        RM.Pages.Functions.Groups_Edit.Request(Ext.getDom('edit_group[id]').value, record.data.field1);
    },
    Save: function(){
        RM.Pages.Groups_Edit_Panel.getForm().submit({
            params: {
                'iso' : Ext.getDom('edit_group_iso_hidden').value
            },
            success: function(form, action) {
                Ext.getCmp('rm_pages_groups_edit_statusbar').setStatus({
                    iconCls: 'ok-icon',
                    text: RM.Translate.Admin.Groups.Edit.Saved
                });
                //RM.Pages.Functions.Groups_ListJson();
            },
            waitMsg: RM.Translate.Common.Saving,
            waitTitle: RM.Translate.Common.PleaseWait
        });
    },
    DeleteRequest : function(){
        var myMask = new Ext.LoadMask('content-panel', {msg:RM.Translate.Common.PleaseWait});
        Ext.Ajax.request({
            url: RM.Common.AssembleURL({
                controller: 'Groups_Edit',
                action: 'deletejson',
                parameters: [{
                    name: 'id',
                    value: Ext.getDom('edit_group[id]').value
                }]
            }),
            method: 'POST',
            success: function(responseObject) {
                myMask.hide();
                RM.Pages.Functions.Groups_ListJson();                  
            },
            failure: function() {
                myMask.hide();
                Ext.Msg.alert(RM.Translate.Common.Status, RM.Translate.Common.UnableToShow);
            }
        });
    },
    Delete: function(){
        Ext.MessageBox.confirm(
            RM.Translate.Common.Delete,
            RM.Translate.Admin.Groups.Edit.DeleteAlert,
            function(buttonID){
                if (buttonID != 'yes') {
                    return;
                }
                RM.Pages.Functions.Groups_Edit.DeleteRequest();
            }
        );
    },
    Cancel: function(){
        RM.Pages.Functions.Groups_ListJson();
    }
}

RM.Pages.Groups_Edit_Toolbar = {
    xtype : "panel",
    id : "rm_pages_groups_edit_toolbar",
    bodyBorder : false,
    html : RM.Common.getToolbar([
        {image: RM.BaseLargeImageURL+"save.gif", label: RM.Translate.Admin.Groups.Edit.Save, link: "RM.Pages.Functions.Groups_Edit.Save()"},
        {image: RM.BaseLargeImageURL+"delete.gif", label: RM.Translate.Admin.Groups.Edit.Delete, link: "RM.Pages.Functions.Groups_Edit.Delete()"},
        {image: RM.BaseLargeImageURL+"back.gif", label: RM.Translate.Common.Back, link: "RM.Pages.Functions.Groups_Edit.Cancel()"}
    ])
};
RM.Toolbars.push(RM.Pages.Groups_Edit_Toolbar);

RM.Pages.Groups_Edit_Language_Combo = new Ext.form.ComboBox({
    id : "edit_groups_iso",
    hiddenName : "edit_group_iso_hidden",
    xtype : "combo",
    cls: 'RM_language_selection_combo',
    typeAhead: true,
    fieldLabel: RM.Translate.Admin.Groups.New.Language,
    forceSelection: true,
    triggerAction: 'all',
    selectOnFocus: true,
    store: RM.Languages,
    autoLoad: true,
    bodyBorder : false,
    frame : false,
    width: 180,
    listeners: {
        'select' : RM.Pages.Functions.Groups_Edit.LanguageChange
    }
});

RM.Pages.Groups_Edit_Language = new Ext.Panel({
    id : "rm_pages_groups_edit_language",
    layout: 'form',
    bodyBorder : false,
    frame : false,
    items : [
        RM.Pages.Groups_Edit_Language_Combo
    ]
});

RM.Pages.Groups_Edit_Title = {
    xtype : "panel",
    id : "rm_pages_groups_edit_title",
    bodyBorder : false,
    frame : false,
    html : "<span class='RM_Title_Language_Bar_img'><img src='"+RM.BaseMenuImageURL+"unit.png'></span>&nbsp;<span class='RM_Title_Language_Bar_text'>"+RM.Translate.Admin.Groups.New.Title+"</span>"
};

RM.Pages.Groups_Edit_Language_Panel = new Ext.Panel({
    id : "rm_pages_groups_edit_language_selection",
    layout: 'column',
    frame : false,
    baseCls: "x-panel-header",
    items : [
        RM.Pages.Groups_Edit_Title,
        RM.Pages.Groups_Edit_Language
    ]
});

RM.Pages.Groups_Edit_Panel_Units_Json = new Ext.data.JsonStore({
    root: 'data',
    totalProperty: 'total',
    fields: [
        {name:'id', type:'int'},
        {name:'name', type:'string'}
    ],
    url: RM.Common.AssembleURL({
        controller: 'Units',
        action: 'listJson'
    })
});

RM.Pages.Groups_Edit_Panel = new Ext.FormPanel({
    xtype: 'form',
    bodyStyle: 'padding: 0 10px 0 10px',
    url : RM.Common.AssembleURL({
        controller: 'Groups_Edit',
        action: 'updatejson'
    }),
    items: [{
        xtype: 'fieldset',
        labelWidth: 150,
        title: RM.Translate.Admin.Groups.New.GroupConfiguration,
        items: [{
            id: 'edit_group[id]',
            name: 'edit_group[id]',
            xtype: 'hidden'
        },{
            id: 'edit_group[name]',
            name: 'edit_group[name]',
            fieldLabel: RM.Translate.Admin.Groups.New.Name,
            allowBlank: false,
            xtype: 'textfield'
        },{
            id : "edit_group_main_unit_id",
            hiddenName: "edit_group[main_unit_id]",
            name: "edit_group_main_unit_id",
            xtype : "combo",
            width: "207",
            allowBlank: false,
            typeAhead: true,
            fieldLabel: RM.Translate.Admin.Groups.New.Unit,
            triggerAction: 'all',
            selectOnFocus: true,
            valueField: 'id',
            displayField: 'name',
            minChars: 1,
            pageSize: 10,
            resizable: true,
            minListWidth: 220,
            store: RM.Pages.Groups_Edit_Panel_Units_Json
        },{
            id : "edit_group_published",
            hiddenName: "edit_group[published]",
            name: "edit_group_published",
            xtype : "combo",
            typeAhead: true,
            fieldLabel: RM.Translate.Common.Published,
            forceSelection: true,
            mode: "local",
            triggerAction: 'all',
            selectOnFocus: true,
            valueField: 'value',
            displayField: 'text',
            allowBlank: false,
            store: new Ext.data.JsonStore({
                fields:[
                    {name:'value', type:'int'},
                    {name:'text', type:'string'}
                ],
                data: RM.Translate.Common.JSON.YesNo
            }),
            labelWidth: 190,
            width: 50
        }]
    }]
});

RM.Pages.Groups_Edit = new Ext.Panel({
    id: "rm_pages_groups_edit",    
    items: [
        RM.Pages.Groups_Edit_Language_Panel,
        RM.Pages.Groups_Edit_Panel
    ],
    bbar: new Ext.ux.StatusBar({
        id: 'rm_pages_groups_edit_statusbar',
        items: []
    })

});

RM.Main.Pages.push(RM.Pages.Groups_Edit);